#include "utypes.h"
#include "queue.h"

void queueInit(Queue *q)
{
	q->in = 0;
	q->out = 0;
	q->cnt = 0; 
	q->size = 10;
}

/** Put char 'c' in the queue pointed to by 'q'
* Returns false if operation failed (the queue is full)
*/
Bool queuePut(Queue *q, char c)
{
	if(q->cnt == q->size)
	{
		//beep();
		return false;
	}
	q->buf[q->in++] = c;
	q->in %= q->size;
	q->cnt++;
	
	return true;
}

/** Get next element from queue
*  Returns -1 (an int) if there are no elements in the queue
*/
int queueGet(Queue *q)
{
	if(!queueEmpty(q))
	{
	if(q->cnt == 0)
		return 0;
	char c = q->buf[q->out++];
	q->out %= q->size;
	q->cnt--;
	return c;
	}
	return 1;
}

/** Returns true if the queue is empty
*/
Bool queueEmpty(Queue *q)
{
	return (q->cnt == 0);
}

/** Returns true if the queue if full
*/
Bool queueFull(Queue *q)
{
	return (q->cnt == q->size);
}

/**
 * makes the queue empty
 */
void clear_queue(Queue *q)
{
	while(!queueEmpty(q))
	{
		queueGet(q);
	}
}

